package com.yourname.xhs.service;

import com.yourname.xhs.dao.UserDaoImpl;
import com.yourname.xhs.entity.User;
import com.yourname.xhs.util.ValidateUtiil;

import java.util.ArrayList;
import java.util.List;


import static com.yourname.xhs.util.DES.desEncript;

public  class UserServiceImpl implements UserService {

    UserDaoImpl udi = new UserDaoImpl();//dao层
    public static User tuser= new User();


    public static String originKey="12345678";//密钥
    List<User> userslist=new ArrayList<>();


    public User login(String Username,String password) throws Exception {
        tuser=null;
        tuser=udi.select(Username);//找到的是加密之后的密码
        System.out.println(tuser);
        if(tuser.getUserId()==0){
            System.out.println("用户名不存在！");

        }
        else {
            if(!ValidateUtiil.validatePassword(tuser,password,originKey)){
                System.out.println("密码错误！");
                return null;
            }
            else {
                return tuser;
            }

        }

        return null;
    }
    public boolean isAdmin(User tuser) throws Exception {
        //如果用户名密码正确 判断管理员
        tuser=udi.select(tuser.getUserName());
        if (udi.isAdmin(tuser)!=null&&udi.isAdmin(tuser).equals("user")){
            tuser.setIsadmin("user");//外加的
            System.out.println("欢迎用户 "+tuser.getUserName());
            return true;
        }
        else {
            tuser.setIsadmin("admin");//外加的
            System.out.println("欢迎管理员"+tuser.getUserName());
            return  false;
        }

    }



 //注册功能
    public boolean register(User user) throws Exception {
         User newuser = new User();
        newuser = udi.select(user.getUserName()); // 查找用户名是否已经存在于数据库中
        System.out.println(newuser);
        if (newuser.getUserId()!= 0) { // 如果找到了已存在的用户
            System.out.println("用户已存在");
            return false; // 注册失败，因为用户名已被占用
        }
         else {
            tuser = user;
            udi.insert(user); // 假设insert方法负责将新用户存入数据库
            return true;
        }


    }

     // 查找个人信息
    public  void getUserInfo(User user) throws Exception {
        user=udi.select(user.getUserName());
        String status=null;
        if(udi.isAdmin(user)!=null&&udi.isAdmin(user).equals("user")){
            status="否";
        }
        else {
            status="是";
        }
        System.out.println("---------------------");
        System.out.println("用户名\t是否为管理员");
        System.out.println(user.getUserName()+"\t\t"+status);
        System.out.println("---------------------");
    }

    //查找所有用户信息
    public  List<User> selectAllUser() throws Exception {
        return udi.selectAllUser();
    }

    //修改个人信息
    public int modifyInfo(User user, String Username) throws Exception {

       return udi.modifyInfo(user,Username);
    }

    //修改密码
    public boolean modifyPassword(User user,String cleartext) throws Exception {
        String password =desEncript(cleartext,originKey);
        user.setPassword(password);
        return udi.modifyPassword(user);
    }

}
